package com.kaikeba.controller;

import com.kaikeba.bean.BootstrapTableUser;
import com.kaikeba.bean.User;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.ResultData;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.ExpressService;
import com.kaikeba.service.UserService;
import com.kaikeba.util.DateFormatUtil;
import com.kaikeba.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author Wuky
 * @Time 2022/1/17 16:48
 * @Version 1.0
 **/
public class UserController {

    @ResponseBody("/user/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        List<Map<String, Integer>> data = UserService.console();
        Message msg = new Message();
        if(data.size() == 0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        int offset = Integer.parseInt(request.getParameter("offset"));
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        List<User> list = UserService.findAll(true, offset, pageNumber);
        int total = UserService.console().get(0).get("data1_size");

        List<BootstrapTableUser> list2 = new ArrayList<>();
        for (User u : list) {
            String registerTime = DateFormatUtil.format(u.getRegisterTime());
            String loginTime = u.getLoginTime() == null?"未激活":DateFormatUtil.format(u.getLoginTime());
            BootstrapTableUser bc = new BootstrapTableUser(u.getId(),u.getNickname(),u.getUserPhone(),u.getPassword(),registerTime,loginTime);
            list2.add(bc);
        }

        ResultData<BootstrapTableUser> data = new ResultData<>();
        data.setRows(list2);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/user/insert.do")
    public String insert(HttpServletRequest request,HttpServletResponse response){
        String nickname = request.getParameter("nickname");
        String userPhone = request.getParameter("userPhone");
        String password = request.getParameter("password");
        User User = new User(nickname, userPhone, password);
        boolean flag = UserService.insert(User);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("用户录入成功！");
        }else{
            msg.setStatus(-1);
            msg.setResult("用户录入失败！");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/find.do")
    public String find(HttpServletRequest request,HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        User u = UserService.findByUserPhone(userPhone);
        Message msg = new Message();
        if (u==null){
            msg.setStatus(-1);
            msg.setResult("用户不存在");
        }else{
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(u);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest request,HttpServletResponse response){
//        手机号
        String userPhone = request.getParameter("userPhone");
//        新用户信息
        String nickname = request.getParameter("nickname");
        String userPhoneNew = request.getParameter("userPhoneNew");
        String password = request.getParameter("password");
        User UserNew = new User(nickname, userPhoneNew, password);
        boolean flag = UserService.update(userPhone, UserNew);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest request,HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        boolean flag = UserService.delete(userPhone);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

}
